//
//  WBComposeTextView.m
//  WeiBoOC
//
//  Created by 韩菲菲 on 16/8/18.
//  Copyright © 2016年 韩菲菲. All rights reserved.
//

#import "WBComposeTextView.h"
#import <Masonry.h>

@interface  WBComposeTextView()

@property (nonatomic , strong) UILabel *placeholderLabel;

@end

@implementation WBComposeTextView

- (instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame])
    {
        [self setupUI];
    
    }
    
    return self;

}
#pragma mark - 重写属性的set方法
- (void)setPlaceholder:(NSString *)placeholder
{
    _placeholder = placeholder;
    
    self.placeholderLabel.text = placeholder;

}

- (void)setTextFont:(UIFont *)textFont
{
    self.font = textFont;
    self.placeholderLabel.font = textFont;
}

- (UILabel *)placeholderLabel
{
    if(!_placeholderLabel)
    {
        
        _placeholderLabel = [[UILabel alloc] init];
        
        //设置换行
        _placeholderLabel.numberOfLines = 0;
        
        //设置文字颜色
        _placeholderLabel.textColor = AppColor;
    
    }
    
    return _placeholderLabel;

}



#pragma mark - 设置UI
- (void)setupUI
{
    //当文本变化时 通知告诉我
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewChanged) name:UITextViewTextDidChangeNotification object:nil];
    
    self.backgroundColor = [UIColor whiteColor];
    
    //添加控件
    [self addSubview:self.placeholderLabel];
    
    //设置约束
    [self.placeholderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(8);
        make.leading.mas_equalTo(5);
        make.width.mas_equalTo(kScreenW - 10);
        
    }];

}

#pragma mark - 通知实现方法
- (void)textViewChanged
{
    //隐藏占位label
    self.placeholderLabel.hidden = [self hasText];

}

@end
